package com.example.fluttert

import android.os.Build
import android.os.Bundle
import android.os.VibrationEffect
import android.os.Vibrator
import android.provider.Settings
import android.content.Context
import android.content.Intent
import android.widget.Toast
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel

/// Android MainActivity
/// 
/// 实现Flutter与Android原生平台的通信
/// 使用MethodChannel处理来自Flutter的方法调用
class MainActivity: FlutterActivity() {
    /// 平台通道名称，必须与Flutter端的channelName一致
    private val CHANNEL = "com.fluttert/native"

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        
        // 创建MethodChannel
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
            when (call.method) {
                // 获取设备信息
                "getDeviceInfo" -> {
                    val deviceInfo = mapOf(
                        "platform" to "Android",
                        "version" to Build.VERSION.RELEASE,
                        "model" to Build.MODEL,
                        "manufacturer" to Build.MANUFACTURER
                    )
                    result.success(deviceInfo)
                }
                
                // 显示Toast消息
                "showToast" -> {
                    val message = call.argument<String>("message") ?: ""
                    val duration = call.argument<Int>("duration") ?: 0
                    
                    Toast.makeText(
                        this,
                        message,
                        if (duration == 1) Toast.LENGTH_LONG else Toast.LENGTH_SHORT
                    ).show()
                    result.success(true)
                }
                
                // 获取电池电量
                "getBatteryLevel" -> {
                    val batteryLevel = getBatteryLevel()
                    result.success(batteryLevel)
                }
                
                // 打开系统设置页面
                "openSettings" -> {
                    val intent = Intent(Settings.ACTION_SETTINGS)
                    startActivity(intent)
                    result.success(true)
                }
                
                // 震动设备
                "vibrate" -> {
                    val duration = call.argument<Int>("duration") ?: 100
                    vibrate(duration)
                    result.success(true)
                }
                
                // 获取应用版本信息
                "getAppVersion" -> {
                    val packageManager = packageManager
                    val packageInfo = packageManager.getPackageInfo(packageName, 0)
                    val versionInfo = mapOf(
                        "versionName" to packageInfo.versionName,
                        "versionCode" to packageInfo.longVersionCode.toString()
                    )
                    result.success(versionInfo)
                }
                
                else -> {
                    result.notImplemented()
                }
            }
        }
    }

    /// 获取电池电量
    private fun getBatteryLevel(): Int {
        val batteryManager = getSystemService(Context.BATTERY_SERVICE) as android.os.BatteryManager
        return batteryManager.getIntProperty(android.os.BatteryManager.BATTERY_PROPERTY_CAPACITY)
    }

    /// 震动设备
    private fun vibrate(duration: Int) {
        val vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            vibrator.vibrate(VibrationEffect.createOneShot(duration.toLong(), VibrationEffect.DEFAULT_AMPLITUDE))
        } else {
            @Suppress("DEPRECATION")
            vibrator.vibrate(duration.toLong())
        }
    }
}

